﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Abp.Authorization.Users;
using Abp.Extensions;
using Abp.Timing;

namespace pandx.Mulan.Authorization.Users
{
    public class User : AbpUser<User>
    {

        public const string DefaultPassword = "123qwe";
        public Guid? ProfilePictureId { get; set; }
        public bool ShouldChangePasswordOnNextLogin { get; set; }
        public DateTime? SignInTokenExpireTimeUtc { get; set; }
        public string SignInToken { get; set; }
        public bool OnlyLockedUsers { get; set; }
        public Gender Gender { get; set; }

        public List<UserOrganizationUnit> OrganizationUnits { get; set; }

        public User()
        {
            IsLockoutEnabled = true;
            PhoneNumber = "";
            OnlyLockedUsers = LockoutEndDateUtc.HasValue && LockoutEndDateUtc.Value > DateTime.UtcNow;

        }

        public override void SetNewPasswordResetCode()
        {
            PasswordResetCode = Guid.NewGuid().ToString("N").Truncate(10).ToUpperInvariant();
        }

        public void Unlock()
        {
            AccessFailedCount = 0;
            LockoutEndDateUtc = null;
        }

        public void SetSignInToken()
        {
            SignInToken = Guid.NewGuid().ToString();
            SignInTokenExpireTimeUtc = Clock.Now.AddMinutes(1).ToUniversalTime();
        }

        public static string CreateRandomPassword()
        {
            return Guid.NewGuid().ToString("N").Truncate(16);
        }

        public static User CreateTenantAdminUser(int tenantId, string emailAddress)
        {
            var user = new User
            {
                TenantId = tenantId,
                UserName = AdminUserName,
                Name = AdminUserName,
                Surname = "",
                EmailAddress = emailAddress,
                Roles = new List<UserRole>(),
                OrganizationUnits = new List<UserOrganizationUnit>()
            };

            user.SetNormalizedNames();

            return user;
        }

    }

    public enum Gender
    {
        [Description("男")]
        Male=0,
        [Description("女")]
        Female=1
    }
}

